Questions
24 of 25
1What built-in HTTP exceptions does NestJS provide out of the box?
2What interface must an exception filter implement and what are the two parameters of catch()?
3How do you apply an exception filter at the method, controller, and global level in NestJS?
4How do you add custom response headers or set cookies inside an exception filter in NestJS?
5How do you unit test an exception filter in NestJS?
6How do you implement a filter that adds a request correlation ID to every error response in NestJS?
7How do getResponse() and getStatus() work on an HttpException and what can getResponse() return?
8How do you create a domain-specific custom exception class in NestJS?
9How does exception filter resolution order work when multiple filters are registered at different levels in NestJS?
10How do you register multiple global filters that each handle a different exception type in NestJS?
11What does the @Catch() decorator do and what happens when you omit its argument?
12How do you extend the built-in BaseExceptionFilter to add logging while keeping default NestJS behaviour?
13How do you handle exceptions differently in a filter depending on whether the request is HTTP, WebSocket, or RPC?
14What are the two ways to register a global exception filter in NestJS and what is the key difference?
15How do interceptors and exception filters divide error-handling responsibility in NestJS?
16How do you write an exception filter that handles GraphQL errors differently from REST errors in NestJS?
17How do you prevent sensitive information from leaking through exception responses in production NestJS apps?
18How do you handle async operations inside an exception filter in NestJS, for example logging to a database?
19What is HttpException in NestJS and what two arguments does its constructor take?
20What does NestJS's built-in global exception layer do when no custom filter is registered?
21What is the cause option on HttpException and how should you use it for error tracing?
22How do you write a production-ready global all-exceptions filter that handles HttpException and unexpected errors differently?
23How do you integrate a third-party error tracker like Sentry inside a global exception filter in NestJS?
24How do you map database and ORM exceptions to HTTP exceptions using a global filter in NestJS?
25How do you re-throw an exception from within a filter to let a higher-scoped filter handle it in NestJS?
24 / 25

How do you map database and ORM exceptions to HTTP exceptions using a global filter in NestJS?

Use @Catch() with no argument to intercept all exceptions including ORM errors. Branch on the exception type to map specific ORM error codes to semantic HTTP status codes — EntityNotFoundError to 404, unique constraint violations (code 23505) to 409, foreign key violations (code 23503) to 400. Log unknown DB errors server-side and return a generic 500.

ORM exception to HTTP exception mapping filter
Common ORM error codes to HTTP status mappings:
  1. 1

    EntityNotFoundError (TypeORM) — map to 404 Not Found.

  2. 2

    QueryFailedError code 23505 (Postgres unique violation) — map to 409 Conflict.

  3. 3

    QueryFailedError code 23503 (Postgres foreign key violation) — map to 400 Bad Request.

  4. 4

    QueryFailedError code 23502 (Postgres not null violation) — map to 400 Bad Request.

  5. 5

    Always log unexpected database errors with the full stack — never silently swallow them.

Difficulty: 6/10
Topics: Exception handling, NestJS filters, ORM integration

Scenario Questions

0-2 years experience
  1. 1

    You have a NestJS controller that calls a TypeORM repository. If the repository throws a QueryFailedError, how would you use a global exception filter to return a 400 Bad Request instead of a 500?

  2. 2

    Walk me through the steps to create a global filter that catches any EntityNotFoundError from TypeORM and maps it to a 404 response.

2-5 years experience
  1. 1

    We noticed that some database constraint violations are bubbling up as 500 errors in production. How would you modify the existing global filter to differentiate between unique constraint violations and foreign key violations, returning appropriate HTTP status codes?

  2. 2

    During a recent feature rollout, a new entity caused a QueryFailedError that wasn't being caught by our filter. What debugging steps would you take to identify why the filter missed it, and how would you fix it?

5-8 years experience
  1. 1

    Our service is part of a larger microservices ecosystem where each service has its own NestJS app. How would you design a reusable global filter library that standardizes DB/ORM exception-to-HTTP mapping across services, considering versioning and shared error contracts?

  2. 2

    When handling high‑throughput API traffic, the global filter adds noticeable latency. What profiling techniques would you use to measure its impact, and what refactoring could reduce overhead while preserving correct error translation?

8+ years experience
  1. 1

    The company plans to migrate from TypeORM to Prisma across multiple teams. How would you evolve the existing global exception filter strategy to accommodate both ORMs during the transition, ensuring consistent HTTP responses and minimal disruption?

  2. 2

    Leadership wants a unified error handling policy that includes logging, alerting, and client‑friendly messages for all data‑layer failures. Describe how you would architect this policy at the organization level, integrating NestJS filters, centralized logging, and cross‑service error schemas.

Follow-up Questions

  • What are the trade‑offs between using NestJS's built‑in HttpException and a custom error class?
  • How would you unit‑test that your global filter correctly translates each ORM exception?
  • In a high‑traffic service, how does the filter interact with other pipes like ValidationPipe?